#pragma once
#include<iostream>
#include<vector>
using namespace std;
namespace kang
{
    template <class T>
    class vector
    {
        typedef T* iterator;
        typedef const T* const_iterator;
    public:
        void push_back(const T& x)
        {
            if (_finish==_end_of_storage)
            {
                reserve(capacity()==0?4:2*capacity());
            }
            *_finish = x;
            _finish++;
        }

        void reserve(size_t n)
        {

            if (n > capacity())
            {
                size_t old_size = size();
                T*tmp = new T[n];
                if (_start)
                memcpy(tmp, _start, sizeof(T)*size());
                delete[]_start;
                _start = tmp;
                _finish = _start + old_size;
                _end_of_storage = _start + n;
            }
        }
        void print_vector(const vector<T>& v)
        {
            //typename vector<T>::const_iterator it = v.begin();
            auto it = v.begin();
            while (it != v.end())
            {
                cout << *it << " ";
                ++it;
            }
            cout << endl;

            for (auto e : v)
            {
                cout << e << " ";
            }
            cout << endl;
        }
        iterator begin()
        {
            return _start;
        }
        iterator end()
        {
            return _finish;
        }
        const iterator begin()const
        {
            return _start;
        }
        const iterator end()const
        {
            return _finish;
        }
        size_t size()
        {
            return _finish - _start;
        }
        size_t capacity()
        {
            return _end_of_storage - _start;
        }
        iterator operator[](size_t i)
        {
            return _start[i];
        }
    private:
        iterator _start = nullptr;
        iterator _finish = nullptr;
        iterator _end_of_storage = nullptr;
    };
    void test()
    {
        vector<int> v;
        v.push_back(1);
        v.push_back(2);
        v.push_back(3);
        v.print_vector(v);
    }
}
